//
//  RecommendVC.swift
//  DouYu
//
//  Created by 王鹏宇 on 2018/4/9.
//  Copyright © 2018年 王鹏宇. All rights reserved.
//

import UIKit

private let kCycleViewH = kScreenW * 3 / 8
private let kGameViewH : CGFloat = 90

class RecommendVC: BaseAnchorVC {
    
    
    //MARK:- 懒加载
    fileprivate lazy var recommendVM : RecommendViewModel = RecommendViewModel()
    fileprivate lazy var cycleView : CycleView = {
        
        let cycleView = CycleView(frame: CGRect(x: 0, y: -(kCycleViewH + kGameViewH), width: kScreenW, height: kCycleViewH))
        
        return cycleView
    }()
    
    fileprivate lazy var gameView : RecommendGameView = {
       
        let gameView = RecommendGameView.recommendGameView()
        gameView.frame = CGRect(x: 0, y: -kGameViewH, width: kScreenW, height: kGameViewH)
        
        
        return gameView
    }()

    override func viewDidLoad() {
        super.viewDidLoad()

       // setupUI()
        // Do any additional setup after loading the view.
       // loadData()
        view.backgroundColor = UIColor.white
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    

    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    }
    */

// MARK: -设置UI界面内容
    
    override func setupUI() {
            
            //1.先调用super.setupUI（）
            super.setupUI()
            
            //2 将CycleView添加到UICollectionView中
            collectionView.addSubview(cycleView)
            
            //3 将gameView添加collectionVIew
            collectionView.addSubview(gameView)
            
            //4 设置collectionView 的内边距
            collectionView.contentInset = UIEdgeInsets(top: kCycleViewH + kGameViewH, left: 0, bottom: 0, right: 0)
    }
    
    //MARK: - 请求数据
    
     override func loadData() {

            // 给父类中的 ViewMode 进行赋值

            baseVM = recommendVM

            // 请求推荐数据
            recommendVM.requestData {

                //1 展示推荐数据
                self.collectionView.reloadData()

                //2 将数据传递给GameView
                var groups = self.recommendVM.anchorGroups
                //2.1 移除前两前

                groups.removeFirst()
                groups.removeFirst()

                groups.remove(at: 1)
                //添加更多数组
                let moreGroup = AnchorGroup()
                moreGroup.tag_name = "更多"
                groups.append(moreGroup)

                self.gameView.groups = groups
                self.loadDataFinished()
            }

        recommendVM.requestCycleData {
            self.cycleView.cycleModels = self.recommendVM.cycleModels
            self.loadDataFinished()
        }
        
        }
}




extension RecommendVC : UICollectionViewDelegateFlowLayout {
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        
        if indexPath.section == 1 {
            
            return CGSize(width: kNormalItemW, height: kPrettyItemH)
        }else {
            return CGSize(width: kNormalItemW, height: kNormalItemH)
        }
    }
    
    override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        
        if indexPath.section == 1 {
            
            let cell = collectionView.dequeueReusableCell(withReuseIdentifier: kPrettyCellID, for: indexPath) as! CollectionPrettyCell
            
            cell.anchor = recommendVM.anchorGroups[indexPath.section].room_list?[indexPath.item]
            
            return cell
        }else {
            
            return super.collectionView(collectionView, cellForItemAt: indexPath)
        }
    }
}
